package com.jz.beijianbao.subclass.service.impl;

import com.jz.beijianbao.subclass.bean.SubClass;
import com.jz.beijianbao.subclass.bean.SubClassAdd;
import com.jz.beijianbao.subclass.bean.SubClassSelectVo;
import com.jz.beijianbao.subclass.mapper.SubMapper;
import com.jz.beijianbao.subclass.service.SubService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SubServiceImpl implements SubService {

    @Autowired
    private SubMapper subMapper;

    @Override
    public ResultVO queryPag(SubClassSelectVo subClassSelectVo) {
        //数据
        List<SubClass> subClasses = subMapper.queryPag(subClassSelectVo);
        for (SubClass sub:subClasses) {
            if (sub.getStatus()){
                sub.setStatusName("启用");
            }else {
                sub.setStatusName("禁用");
            }
        }
        //数量
        int count = subMapper.queryCount(subClassSelectVo);
        //返回
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(subClasses);
        allRecordsVo.setPageIndex(subClassSelectVo.getPageIndex());
        allRecordsVo.setPageSize(subClassSelectVo.getPageSize());
        allRecordsVo.setTotalPage(allRecordsVo.countPage(count));
        allRecordsVo.setTotalSize(count);
        return ResultBuildVO.success(allRecordsVo);
    }

    @Override
    public ResultVO add(SubClassAdd subClassAdd) {
        SubClassAdd code = subMapper.queryByCode(subClassAdd.getCode());
        if (code!=null){
            return ResultBuildVO.error("编码已经存在");
        }
        SubClassAdd name = subMapper.queryByName(subClassAdd.getName());
        if (name!=null){
            return ResultBuildVO.error("名称已经存在");
        }
        Date date = new Date();
        SubClass subClass = new SubClass();
        BeanUtils.copyProperties(subClassAdd,subClass);
        subClass.setCreateTime(date);
        subClass.setCreateName("通过session获取用户名称");
        int add = subMapper.add(subClass);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO selectById(Integer id) {
        subMapper.selectById(id);
      return   ResultBuildVO.success();

    }

    @Override
    public ResultVO update(SubClassAdd subClassAdd) {


        int version = subMapper.update(subClassAdd);
        if (version==0){
           return ResultBuildVO.error("请刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO status(SubClass subClass) {
          subMapper.status(subClass);
        return   ResultBuildVO.success();
    }
}
